<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Common\Server;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Contract\TranslatorInterface;
use App\Common\Demand\Server\Admin as AdminInterfaces;
use App\Common\Demand\Models\Admin as AdminModel;
use App\Common\Demand\Server\Utils;
use App\Common\Demand\Server\LoginLog;
use App\Common\Exception\Store\{LoginException,DataNotFound};
class Admin implements AdminInterfaces
{
    #[Inject]
    protected  AdminModel $model;
    #[Inject]
    protected  Utils $utils;
    #[Inject]
    protected  LoginLog $log;
    #[Inject]
    protected  TranslatorInterface $translatorInterface;
    public function getListPage(array $requestData, array $field=['*']):array{
        return $this->model->getListPage($requestData,$field);
    }
    public function getList(array $where, array $field=['*']):array{
        return $this->model->getList($where,$field);
    }
    public function saveAdd($addData):bool{
        $addData['password']=password_hash($addData['password'],PASSWORD_DEFAULT);
        $addData['is_chat']=0;
        $addData['status']=1;
        return $this->model->saveAdd($addData);
    }
    public function getRow($where, array $field=['*']):array{
        return $this->model->getRow($where,$field);
    }
    public function saveEdit($where,$editData):bool{
        return $this->model->saveEdit($where,$editData);
    }
    public function saveDel($where):bool{
        return $this->model->saveDel($where);
    }
    public function login($loginData):array{
        try {
            $where[]=['account','=',$loginData['account']];
            $row=$this->getRow($where);
            if(!$this->log->checkLog($row['admin_id'],1)){
                throw new LoginException($this->translatorInterface->trans('admin.errorMax'));
            }
            if (password_verify($loginData['password'], $row['password'])) {
                return ['token'=>$this->log->LoginSuccess($row['admin_id'],1)];
            } else {
                $this->log->LoginError($row['admin_id'],1);
                throw new LoginException($this->translatorInterface->trans('admin.errorPassword'));
            }
        } catch (DataNotFound $th) {
            throw new LoginException($this->translatorInterface->trans('admin.accountNot'));
        }
        
    }
    public function saveInfo($adminId,$infoData):bool{
        return true;
    }
    public function resetPassword($adminId,$newPassword):bool{
        return true;
    }
    public function logout($adminId):bool{
        return true;
    }
}